/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—07—05
 * Time: 13:54
 */
public class TestSingleList {
    //无头单向非循环链表实现
    static class Node{
        public int val;
        public Node next;
        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;
    //头插法 时间复杂度O(1)
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

    //尾插法 时间复杂度O(n)
    public void addLast(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
        }else {
            Node cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }
    //判断index位置合法性
    private void checkIndex(int index) {
        if (index < 0 || index > size()) {
            throw new IndexNotLegalException("index位置不合法");
        }
    }
    //让cur走到 index前一个结点
    private Node findIndexSubOfOne(int index) {
        Node cur = head;
        while(index-1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        checkIndex(index);
        if (index == 0) {//头插法
            addFirst(data);
            return;
        }
        if (index == size()) {//尾插法
            addLast(data);
            return;
        }
        Node node = new Node(data);
        Node cur = findIndexSubOfOne(index);
        node.next = cur.next;
        cur.next = node;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur =head;
        while(cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //找到所以删除结点的前驱
    private Node searchPrevOfKey(int key) {
        Node cur = this.head;
        while(cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        //如果删除头结点
        if (head.val == key) {
            head = head.next;
            return;
        }
        Node cur = searchPrevOfKey(key);
        if (cur == null) return;
        Node del = cur.next;
        cur.next = del.next;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if (head == null) return;
        Node prev = head;
        Node cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.val == key) {
            head= head.next;
        }
    }

    //得到单链表的长度
    public int size() {
        Node cur =head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        while(this.head != null) {
            System.out.print(this.head.val+" ");
            this.head = this.head.next;
        }
        System.out.println();
    }
   //清空链表
    public void clear() {
        this.head = null;
/*        Node cur = head;
        while(cur != null) {
            Node curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        head = null;*/
    }
}
